<template>
  <div class="login-body">
    <div class="login-panel">
      <div class="login-title">用户登录</div>
      <el-form :model="formData" :rules="rules" ref="formDataRef">
        <el-form-item prop="account">
          <el-input
            placeholder="请输入账号"
            v-model="formData.account"
            size="large"
          >
            <template #prefix>
              <span class="iconfont icon-zhanghu"></span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="formData.password"
            size="large"
          >
            <template #prefix>
              <span class="iconfont icon-password"></span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="checkCode">
          <div class="check-code-panel">
            <el-input
              placeholder="请输入验证码"
              v-model="formData.checkCode"
              class="input-panel"
              @keyup.enter.native="login"
              size="large"
            />
            <img
              :src="checkCodeUrl"
              class="check-code"
              @click="changeCheckCode"
            />
          </div>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="formData.remeberMe" :true-value="1"
            >记住我</el-checkbox
          >
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" :style="{ width: '100%' }" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
// 组件逻辑
import { getCurrentInstance, reactive, ref } from "vue";
import md5 from "js-md5";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();
const router = useRouter();
const api = {
  checkCode: "api/checkCode",
  login: "/login",
};
const checkCodeUrl = ref(api.checkCode);
const changeCheckCode = () => {
  checkCodeUrl.value = api.checkCode + "?" + new Date().getTime();
};

//表单
const formDataRef = ref(null);
const formData = reactive({});
const rules = {
  account: [
    {
      required: true,
      message: "请输入用户名称",
    },
  ],
  passwrod: [
    {
      required: true,
      message: "请输入密码",
    },
  ],
  checkCode: [
    {
      required: true,
      message: "请输入验证码",
    },
  ],
};
const init = () => {
  const loginInfon = proxy.VueCookies.get("loginInfo");
  if (!loginInfon) {
    return;
  }
  Object.assign(formData, loginInfon);
};
init();
const login = () => {
  formDataRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }
    let cookieloginInfo = proxy.VueCookies.get("loginInfon");
    let cookiePassword =
      cookieloginInfo == null ? null : cookieloginInfo.password;
    if (formData.password !== cookiePassword) {
      formData.password = md5(formData.password);
    }

    let params = {
      account: formData.account,
      password: formData.password,
      checkCode: formData.checkCode,
    };
    let result = await proxy.Request({
      url: api.login,
      params: params,
      errorCallback: () => {
        changeCheckCode();
      },
    });
    if (!result) {
      return;
    }
    proxy.Message.success("登录成功");
    setTimeout(() => {
      router.push("/");
    }, 1500);
    const loginInfon = {
      account: params.account,
      password: params.password,
      remeberMe: formData.remeberMe,
    };
    proxy.VueCookies.set("userInfo", result.data, 0);
    if (formData.remeberMe == 1) {
      proxy.VueCookies.set("loginInfon", loginInfon, "7d");
    }
  });
};
</script>

<style lang="scss">
/* 组件样式 */
.login-body {
  width: 100%;
  height: calc(100vh);
  background-size: cover;
  background-position: center;
  background-image: url("../assets/Login_bg2.jpg");
  .login-panel {
    width: 350px;
    // height: 20px;
    background: #fff;
    float: right;
    margin-right: 100px;
    margin-top: 100px;
    border-radius: 5px;
    padding: 20px;
    box-shadow: 2px 2px 10px #ddd;
    background: rgba(225, 255, 255, 0.6);
    .login-title {
      font-size: 25px;
      text-align: center;
      margin-bottom: 10px;
    }
    .check-code-panel {
      // width: 100%;
      display: flex;
      align-items: center;
      .input-panel {
        flex: 1;
        margin-right: 5px;
      }
      .check-code {
        cursor: pointer;
      }
    }
  }
}
</style>
